package com.railway.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.Station;
import com.railway.dao.StationDao;
import com.railway.util.DBUtil;

public class StationDaoImpl implements StationDao {

	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	
	@Override
	public int insertStation(Station s) {
		String sql = "insert into station(station_name,station_city,is_quit) values(?,?,1)";
		return jt.update(sql,s.getStation_name(),s.getStation_city());
	}

	@Override
	public int deleteStation(int station_id) {
		String sql ="update station set is_quit = 0 where station_id = ?";
		return jt.update(sql,station_id);
	}

	@Override
	public List<Station> selectStation(String station_name) {
		String sql = "select * from station where station_name = ?";
		List<Station> li = jt.query(sql, new BeanPropertyRowMapper<Station>(Station.class),station_name);
		if(li.size() == 0) {
			return null;
		}else {
			return li;
		}
	}

	@Override
	public int updateStation(Station s) {
		String sql = "update station set stationDetailed = ? where station_id = ?";
		return jt.update(sql,s.getStation_id(),s.getStationDetailed());
	}

	@Override
	public List<Station> selectStationAll(int pageNum, int pageSize) {
		String sql = "select * from station where station_name = ? limit ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<Station>(Station.class),pageNum,(pageNum-1)*pageSize);
	}

	@Override
	public int countStation() {
		String sql = "select count(1) from station";
		return jt.queryForObject(sql, Integer.class);
	}

}
